package main.java.com.web;

import main.java.com.entity.Category;
import main.java.com.entity.Dish;
import main.java.com.service.CategoryService;
import main.java.com.service.DishService;
import main.java.com.service.impl.CategoryServiceImpl;
import main.java.com.service.impl.DishServiceImpl;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
//修改页面
@WebServlet("/EditDishServlet")
public class EditDishServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // 设置请求和响应的字符编码
    request.setCharacterEncoding("UTF-8");
    response.setContentType("text/html; charset=UTF-8");

    // 从请求中获取菜品id，用于后续查询
    Long id = Long.parseLong(request.getParameter("id"));

    // 创建DishService实例，用于调用获取菜品信息的方法
    DishService dishService = new DishServiceImpl();
    Dish dish = dishService.getDishById(id);

    // 创建CategoryService实例，用于获取所有分类信息
    CategoryService categoryService = new CategoryServiceImpl();
    List<Category> categories = categoryService.getAllCategories();

    // 将菜品和分类信息设置为请求属性
    request.setAttribute("dish", dish);
    request.setAttribute("categories", categories);
    // 转发请求到editDish.jsp页面，进行菜品信息的编辑
    request.getRequestDispatcher("/backend/editDish.jsp").forward(request, response);
    }

    // 处理菜品信息更新的POST请求
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置请求和响应的字符编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");

        // 从请求中获取菜品信息的各个字段值
        Long id = Long.parseLong(request.getParameter("id"));
        String name = request.getParameter("name");
        Long category_id = Long.parseLong(request.getParameter("category_id"));
        String priceStr = request.getParameter("price");
        BigDecimal price = new BigDecimal(priceStr);
        String image = request.getParameter("image");
        String description = request.getParameter("description");
        Integer status = Integer.parseInt(request.getParameter("status"));

        // 创建Dish实例并设置其属性值
        Dish dish = new Dish();
        dish.setId(id);
        dish.setName(name);
        dish.setCategory_id(category_id);
        dish.setPrice(price);
        dish.setImage(image);
        dish.setDescription(description);
        dish.setStatus(status);

        // 创建DishService实例，用于调用更新菜品信息的方法
        DishService dishService = new DishServiceImpl();
        boolean updateSuccess = dishService.updateDish(dish);

        // 根据更新结果设置提示信息并转发到相应的页面
        if (updateSuccess) {
            // 如果更新成功，设置提示信息并转发到菜品列表页面（可根据实际情况调整跳转逻辑）
            request.setAttribute("updateSuccess", "菜品信息修改成功！");
            request.getRequestDispatcher("/DishListServlet").forward(request, response);
        } else {
            // 如果更新失败，设置错误信息并重新转发到编辑页面
            request.setAttribute("updateError", "菜品信息修改失败，请重试！");
            request.getRequestDispatcher("/backend/editDish.jsp").forward(request, response);
        }
    }
}
